package com.ruix.app.network;

import android.text.TextUtils;

import com.ruix.app.util.LogUitl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by hadexs on 2017/4/12.
 */

public class RequestParams {
    private long requestTime;// 请求时间
    private String url;

    protected String tag;

    protected String host;// 接口地址
    protected String path;// 接口方法
//    protected StringBuffer getParams;// get方式参数

    protected JSONObject getParameters;//
    protected JSONObject parameters; //api方法入参

    protected JSONObject postFormParams;
//    protected Map<String, String> files;
//    protected String mediaType;

    public String getUrl() {
        if (url == null) {
            url = host + path;
        }
        if (getParameters != null && !TextUtils.isEmpty(url)) {
            StringBuffer sb = new StringBuffer();
            if (!url.contains("?")) {
                sb.append("?");
            }
            Iterator<String> keys =  getParameters.keys();
            if (keys != null) {
                while (keys.hasNext()) {
                    String key = keys.next();
                    sb.append("&");
                    sb.append(key);
                    sb.append("=");
                    sb.append(getParameters.optString(key));
                }
            }
            return url + sb.toString();
        } else {
            return url;
        }

    }

    public RequestParams addGetParameter(String key, Object value) {
        if (getParameters == null) {
            getParameters = new JSONObject();
        }
        try {
            getParameters.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    public RequestParams addParameter(String key, Object value) {
        if (parameters == null) {
            parameters = new JSONObject();
        }
        try {
            parameters.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    public RequestParams addFormParameter(String key, Object value) {
        if (postFormParams == null) {
            postFormParams = new JSONObject();
        }
        try {
            postFormParams.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }



//    public RequestParams addFile(String key, String value) {
//        if (files == null) {
//            files = new HashMap<>();
//        }
//        files.put(key, value);
//        return this;
//    }

    public JSONObject getGetParameters() {
        return getParameters;
    }

    public JSONObject getPostFormParams() {
        return postFormParams;
    }

    public JSONObject getParameters() {
        return parameters;
    }

//    public HashMap getFiles() {
//        return files;
////    }


    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
